﻿using System.Configuration;
using QFXProxy.Model;
using QFXProxy.View;

namespace QFXProxy.Presenter
{
    public class ConverterPresenter
    {
        private readonly IConverterView _view;
        private readonly IFileConverter _converter;

        public IConverterView view
        {
            get { return _view; }
        }

        /// <summary>
        /// Constructor called by the view.
        /// </summary>
        /// <param name="view">Object that represents the view of this presenter.</param>
        /// <param name="converter">Object that will perform the file conversion.</param>
        public ConverterPresenter(IConverterView view, IFileConverter converter)
        {
            _view = view;
            _converter = converter;
        }

        /// <summary>
        /// Perform the file conversion.
        /// </summary>
        public void ConvertFile()
        {
            // If the source file is already initialized, then
            // automatically initiates the conversion.
            if (!string.IsNullOrEmpty(_view.SourceFile))
            {
                _converter.SourceFile = _view.SourceFile;
                _converter.Convert();
                System.Diagnostics.Process.Start(_converter.DestinationFile);
                _view.CloseView();
            }
        }

        /// <summary>
        /// Respond to events when the view closes.
        /// </summary>
        public void OnCancel()
        {
            _view.CloseView();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        public void InitializeViewData(string[] args)
        {
            // Initialize the source file from arguments.
            if (args.Length > 0)
            {
                _view.SourceFile = args[0];
            }

            // Initialize the bank id from the configuration file.
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings["INTUIT.BID"] != null &&
                config.AppSettings.Settings["INTUIT.BID"].Value.Length > 0)
            {
                _view.BankId = config.AppSettings.Settings["INTUIT.BID"].Value;
            }
        }
    }
}
